#!/usr/bin/perl -w

use strict;
use CGI;
use LWP::Simple;
use XML::Simple;
use JSON;

# Yahoo! JAPAN Web Search API
my $YJWS_APPID = "[insert your AppID]";
my $YJWS_API_URL = 'http://api.search.yahoo.co.jp/WebSearchService/V1/webSearch';

# Pre-construct objects
my $parser = XML::Simple->new();
#my $json = JSON->new(pretty => 1, delimiter => 0);
my $json = JSON->new();

my $q = new CGI;
{
    my $query = $q->param('query');
    if (!$query) {
	show_form();
	exit;
    }

    # construct URL for Yahoo! API
    my $url = "$YJWS_API_URL?appid=$YJWS_APPID&query=$query";
    for my $param (qw/type results start format adult_ok similar_ok language country site/) {
	$url .= '&' . $param . '=' . $q->param($param)
	    if defined($q->param($param)) && $q->param($param);
    }

    # fetch and parse
    my $res = get($url);
    if ($res =~ /<Error /) {
	print "Content-Type: text/plain\n\n";
	exit;
    }
    my $data = $parser->XMLin($res);

    # drop garbage
    for my $col (qw/xmlns xmlns:xsi xsi:schemaLocation/) {
	delete $data->{$col};
    }
    if ($data->{totalResultsReturned} == 1) {
	$data->{Result} = [ $data->{Result} ];
    }
    for my $result (@{$data->{Result}}) {
	for my $col (qw/MimeType ModificationDate Cache ClickUrl/) {
	    delete $result->{$col};
	}
    }

    # render to JSON
    print "Content-Type: text/plain\n\n";
    print $json->objToJson($data);
}

sub show_form {
    print << "EOD";
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Yahoo! JAPAN Web Search (JSON)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body { font-size: 80%; }
dt { clear: both; }
dd { float: left; }
</style>
</head>
<body>
<h1>Yahoo! JAPAN Web Search (JSON)</h1>
<form method="get" action="./ysearch-json.cgi" />
<dl>

<dt>query:</dt>
<dd><input type="text" name="query" /></dd>

<dt>type: (default: all)</dt>
<dd><input type="radio" name="type" value="all" checked="checked" />all</dd>
<dd><input type="radio" name="type" value="any" />any</dd>
<dd><input type="radio" name="type" value="phrase" />phrase</dd>

<dt>results: (default: 10, max: 50)</dt>
<dd><input type="text" name="results" value="10" /></dd>

<dt>start: (default: 1)</dt>
<dd><input type="text" name="start" value="1" /></dd>

<dt>format: (default: any)</dt>
<dd><input type="radio" name="format" value="any" checked="checked" />any</dd>
<dd><input type="radio" name="format" value="html" />html</dd>
<dd><input type="radio" name="format" value="msword" />msword</dd>
<dd><input type="radio" name="format" value="pdf" />pdf</dd>
<dd><input type="radio" name="format" value="ppt" />ppt</dd>
<dd><input type="radio" name="format" value="rss" />rss</dd>
<dd><input type="radio" name="format" value="txt" />txt</dd>
<dd><input type="radio" name="format" value="xls" />xls</dd>

<dt>adult_ok: (default: none)</dt>
<dd><input type="text" name="adult_ok" value="" /></dd>

<dt>similar_ok: (default: none)</dt>
<dd><input type="text" name="similar_ok" value="" /></dd>

<dt>language: (default: ja)</dt>
<dd><input type="text" name="language" value="ja" /> (<a href="http://developer.yahoo.co.jp/search/languages.html">supported languages</a>)</dd>

<dt>country: (default: none)</dt>
<dd><input type="text" name="country" value="" /> (<a href="http://developer.yahoo.co.jp/search/countries.html">supported countries</a>)</dd>

<dt>site: (default: none)</dt>
<dd><input type="text" name="site" value="" /></dd>
</dl>
<p style="clear: both"><input type="submit" value="search" /></p>
</form>
<p>SEE ALSO: <a href="http://developer.yahoo.co.jp/search/web/V1/webSearch.html">http://developer.yahoo.co.jp/search/web/V1/webSearch.html</a></p>
</body>
</html>
EOD
}
